<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('权限修改')"/>
</head>
<body>
<form action="" class="layui-form">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">
                <div class="layui-form-item layui-hide" th:if="${sysPower.id != null}">
                    <label class="layui-form-label">编号</label>
                    <div class="layui-input-block">
                        <input autocomplete="off" class="layui-input" lay-verify="required" name="id"
                               th:value="${sysPower.id}" type="text">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">父级</label>
                    <div class="layui-input-block">
                        <ul class="dtree" data-id="-1" id="selectParent" name="parentId"></ul>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">名称</label>
                    <div class="layui-input-block">
                        <input autocomplete="off" class="layui-input" lay-verify="required" name="title"
                               placeholder="权限名称" th:value="${sysPower.title}" type="text">
                    </div>
                </div>
                <div class="layui-form-item" id="powerCodeItem">
                    <label class="layui-form-label">标识</label>
                    <div class="layui-input-block">
                        <input autocomplete="off" class="layui-input" id="menuCode" name="menuCode"
                               placeholder="权限标识" th:value="${sysPower.menuCode}" type="text">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">状态</label>
                    <div class="layui-input-block">
                        <select id="selStatus" name="status">
                            <option value="0" th:selected="${sysPower.status eq 0}">正常</option>
                            <option value="-1" th:selected="${sysPower.status eq -1}">删除</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">类型</label>
                    <div class="layui-input-block">
                        <input lay-filter="menuType" name="menuType" th:checked="${sysPower.menuType eq 0}"
                               title="目录" type="radio" value="0">
                        <input lay-filter="menuType" name="menuType" th:checked="${sysPower.menuType eq 1}"
                               title="菜单" type="radio" value="1">
                        <input lay-filter="menuType" name="menuType" th:checked="${sysPower.menuType eq 2}"
                               title="按钮" type="radio" value="2">
                    </div>
                </div>
                <div class="layui-form-item" id="powerUrlItem">
                    <label class="layui-form-label">路径</label>
                    <div class="layui-input-block">
                        <input autocomplete="off" class="layui-input" id="txturl" name="url"
                               placeholder="菜单路径" th:value="${sysPower.url}" type="text"/>
                    </div>
                </div>
                <div class="layui-form-item" id="openTypeItem">
                    <label class="layui-form-label">打开</label>
                    <div class="layui-input-block">
                        <select id="openType" name="openType">
                            <option value=""></option>
                            <option th:selected="${sysPower.openType eq '_iframe'}" value="_iframe">框架</option>
                            <option th:selected="${sysPower.openType eq '_blank'}" value="_blank">签页</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">图标</label>
                    <div class="layui-input-block">
                        <input class="layui-input" id="icon" lay-filter="icon" name="icon" th:value="${sysPower.icon}"
                               type="text">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">排序</label>
                    <div class="layui-input-block">
                        <input autocomplete="off" class="layui-input" name="sort" placeholder="排序"
                               th:value="${sysPower.sort}" type="text">
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button class="pear-btn pear-btn-primary pear-btn-sm" lay-filter="power-save" lay-submit="" type="submit">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button class="pear-btn pear-btn-sm" type="reset">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
<th:block th:include="include :: footer"/>
<script type="text/javascript">

</script>
<script th:inline="javascript">
    layui.use(['form', 'jquery', 'iconPicker', 'dtree'], function () {
        //***************************************************** 变量定义 ******************************************//
        let form = layui.form;
        let $ = layui.jquery;
        let iconPicker = layui.iconPicker;
        let dtree = layui.dtree;

        //***************************************************** 函数定义 ******************************************//

        //***************************************************** 方法调用 ******************************************//

        //***************************************************** 事件触发 ******************************************//
        iconPicker.render({
            elem: '#icon',
            type: 'fontClass',
            search: true,
            page: true,
            limit: 16
        });

        dtree.renderSelect({
            elem: "#selectParent",
            url: contextPath + "/system/power/selectParent",
            method: 'post',
            selectInputName: {nodeId: "parentId", context: "title"},
            //skin: "layui", //这种样式不会有选中的效果
            line: true, // 有线树
            record: true,//开启参数里面增加 recordData，这里存放的是当前数据库的数据
            dataFormat: "list",
            response: {treeId: "id", parentId: "parentId", title: "title"},
            selectInitVal: "[[${sysPower.parentId}]]",//注意这边需要是字符串，不然默认初始值不会赋值
            done: function (res, $ul, first) {
                if (first) {
                    //window.init([[${sysPower.menuType}]]);
                }
            }
        });

        form.on("radio(menuType)", function (data) {
            if (this.value === '0') {
                $("#powerUrlItem").hide();
                $("#powerCodeItem").hide();
                $("#openTypeItem").hide();
                // $("#url").val("");
                // $("#menuCode").val("");
                // $("#openType").val("");
            } else if (this.value === '1') {
                $("#powerUrlItem").show();
                $("#powerCodeItem").show();
                $("#openTypeItem").show();
            } else if (this.value === '2') {
                $("#powerUrlItem").show();
                $("#openTypeItem").hide();
                $("#powerCodeItem").show();
                // $("#url").val("");
                // $("#openType").val("");
            }
        });

        form.on('submit(power-save)', function (data) {
            let _type = 'post';
            if(data.field.id){_type= 'put';}
            data.field.icon = "layui-icon " + data.field.icon;
            data.field.selectParent_select_input = undefined;
            $.ajax({
                //url: contextPath + '/system/power/edit',
                url: createLink(ActionGUID),
                data: data.field,
                type: _type,
                //data: JSON.stringify(data.field),
                //dataType: 'json',
                //contentType: 'application/json',
                //type: 'put',
                success: function (result) {
                    if (result.success) {
                        layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                            parent.layui.treetable.reload("#power-table");
                        });
                    } else {
                        layer.msg(result.msg, {icon: 2, time: 1000});
                    }
                }
            })
            return false;
        });

        window.init = function (type) {
            if (type === 0) {
                $("#powerUrlItem").hide();
                $("#powerCodeItem").hide();
                $("#openTypeItem").hide();
                $("#url").val("");
                $("#menuCode").val("");
                $("#openType").val("");
            } else if (type === 1) {
                $("#powerUrlItem").show();
                $("#powerCodeItem").show();
                $("#openTypeItem").show();
            } else if (type === 2) {
                $("#powerUrlItem").show();
                $("#openTypeItem").hide();
                $("#powerCodeItem").show();
                $("#url").val("");
                $("#openType").val("");
            }
        }

        window.init([[${sysPower.menuType}]]);

        /**
         * 点击菜单状态按钮时，会相应显示或隐藏组件
         */
        form.on("radio(menuType)", function () {
            if (this.value == '0') {
                $("#powerUrlItem").hide();
                $("#powerCodeItem").hide();
                $("#openTypeItem").hide();
                $("#url").val("");
                $("#menuCode").val("");
                $("#openType").val("");
            } else if (this.value == '1') {
                $("#powerUrlItem").show();
                $("#powerCodeItem").show();
                $("#openTypeItem").show();
            } else if (this.value == '2') {
                $("#powerUrlItem").show();
                $("#openTypeItem").hide();
                $("#powerCodeItem").show();
                $("#url").val("");
                $("#openType").val("");
            }
        });

        // 绑定节点点击
        dtree.on("node('selectParent')", function (obj) {
            //layer.msg(JSON.stringify(obj));
            //根据后台返回菜单类型展示不同的选项
            let menuType = obj.param.recordData.menuType;
            if (menuType == 0) { //目录
                $("input[name='menuType'][value='0']").prop("checked", true);
                $("input[name='menuType'][value='0']").next('.layui-form-radio').show();
                $("input[name='menuType'][value='1']").next('.layui-form-radio').show();
                $("input[name='menuType'][value='2']").next('.layui-form-radio').hide();
            } else if (menuType == 1) //菜单
            {
                $("input[name='menuType'][value='2']").prop("checked", true);
                $("input[name='menuType'][value='0']").next('.layui-form-radio').hide();
                $("input[name='menuType'][value='1']").next('.layui-form-radio').hide();
                $("input[name='menuType'][value='2']").next('.layui-form-radio').show();
            } else {
                $("input[name='menuType'][value='0']").prop("checked", true);
                $("input[name='menuType'][value='0']").next('.layui-form-radio').show();
                $("input[name='menuType'][value='1']").next('.layui-form-radio').show();
                $("input[name='menuType'][value='2']").next('.layui-form-radio').hide();
            }
            //form.render();
            $(':radio[lay-filter="menuType"]:checked').next('.layui-form-radio').click();

            //layer.msg(JSON.stringify(obj.param));
            // console.log(obj.param); // 点击当前节点传递的参数
            // console.log(obj.dom); // 当前节点的jquery对象
            // console.log(obj.childParams); // 当前节点的所有子节点参数
            // console.log(obj.parentParam); // 当前节点的父节点参数
        });
    })
</script>
<script>
</script>
</body>
</html>